package cn.tedu.ems.config;

import cn.tedu.ems.mybatis.InsertUpdateTimeInterceptor;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;

import javax.annotation.PostConstruct;
import java.util.List;

/**
 * MyBatis配置类
 *
 * @Author java.@Wqy
 * @Version 0.0.1
 */
@Slf4j
@Configuration
@MapperScan("cn.tedu.ems.mapper")
public class MybatisConfiguration {

    //测试配置是否加载成功,输出对应日志内容
    public MybatisConfiguration(){
        log.info("创建配置类对象:MybatisConfiguration");
    }

    @Autowired
    private List<SqlSessionFactory> sqlSessionFactoryList;

    // 暂时不使用拦截器,后面再用!!!
    @PostConstruct
    public void addInterceptor(){
        log.debug("开始注册Mybatis拦截器");
        InsertUpdateTimeInterceptor interceptor = new InsertUpdateTimeInterceptor();
        for (SqlSessionFactory sqlSessionFactory : sqlSessionFactoryList) {
            sqlSessionFactory.getConfiguration().addInterceptor(interceptor);// 将拦截器传入回话工厂,进行监听
        }
    }
}
